﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class success : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindTreeNode();
    }

    /// <summary>
    /// 创建数据库连接
    /// </summary>
    /// <returns></returns>
    private SqlConnection CreateConn()
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
        SqlConnection conn = new SqlConnection(connStr);

        return conn;
    }

    /// <summary>
    /// 从tb_Department获取FID等于n的DataSet对象
    /// </summary>
    /// <returns></returns>
    private DataSet getDataSet()
    {
        SqlConnection conn = CreateConn();
        DataSet ds = new DataSet();

        try
        {
            string SelectComdStr = "SELECT * FROM tb_Department";
            SqlCommand selectCmd = new SqlCommand(SelectComdStr, conn);

            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = selectCmd;

            da.Fill(ds);
            conn.Close();
        }
        catch (Exception ex)
        {
            conn.Close();
        }

        return ds;

    }

    /// <summary>
    /// 绑定数据到树
    /// </summary>
    /// <param name="treeView">绑定数据的树</param>
    /// <param name="dt">数据源表</param>
    private void BindTree(TreeView treeView, DataTable dt, int partID)
    {
        treeView.Nodes.Clear();//清空节点

        DataRow[] rows = dt.Select("ID=" + partID + "");
        if (rows.Length < 0)
        {
            return;//找不到根节点就返回
        }

        /*TreeNode FatherNode = new TreeNode();
        foreach (DataRow row in rows)
        {
            FatherNode.Text = row["Department"].ToString();
            FatherNode.Value = row["ID"].ToString();
            TreeView1.Nodes.Add(FatherNode);
            CreateChildNode(FatherNode, dt);
        }*/

        TreeNode root = new TreeNode();
        root.Text = rows[0]["Department"].ToString();
        root.Value = rows[0]["ID"].ToString();
        TreeView1.Nodes.Add(root);
        CreateChildNode(root, dt);

        root.Expand();
    }

    /// <summary>
    /// 创建子节点
    /// </summary>
    /// <param name="treeNode">父节点</param>
    /// <param name="dt">存放子节点的数据表</param>
    private void CreateChildNode(TreeNode treeNode, DataTable dt)
    {
        DataRow[] rows = dt.Select("FID='" + treeNode.Value + "'");
        foreach (DataRow row in rows)
        {
            TreeNode Node = new TreeNode();
            Node.Text = row["Department"].ToString();
            Node.Value = row["ID"].ToString();
            treeNode.ChildNodes.Add(Node);
            CreateChildNode(Node, dt);
        }
    }

    /// <summary>
    /// 绑定树节点
    /// </summary>
    private void BindTreeNode()
    {
        DataSet ds = getDataSet();
        DataTable dt = ds.Tables[0];
        BindTree(TreeView1, dt, 1);
    }
}